package com.compass.doctor.ui.chat.viewholder;


import static com.compass.framework.utils.ResUtilsKt.getDrawableResource;

import android.annotation.SuppressLint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.compass.doctor.R;
import com.compass.doctor.databinding.ChatMessageInquiryViewHolderBinding;
import com.compass.doctor.preview.PreviewPicActivity;
import com.compass.doctor.ui.chat.ImDrugAdapter;
import com.compass.doctor.ui.chat.ImPictureAdapter;
import com.compass.doctor.ui.chat.attachment.InquiryAttachment;
import com.compass.framework.model.ImDrug;
import com.compass.framework.utils.FullyGridLayoutManager;
import com.compass.framework.utils.LogUtil;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.luck.picture.lib.entity.LocalMedia;
import com.netease.yunxin.kit.chatkit.ui.common.MessageHelper;
import com.netease.yunxin.kit.chatkit.ui.databinding.ChatBaseMessageViewHolderBinding;
import com.netease.yunxin.kit.chatkit.ui.fun.view.message.viewholder.FunChatBaseMessageViewHolder;
import com.netease.yunxin.kit.chatkit.ui.model.ChatMessageBean;
import com.netease.yunxin.kit.chatkit.ui.view.message.viewholder.ChatBaseMessageViewHolder;
import com.netease.yunxin.kit.common.utils.SizeUtils;

import java.lang.reflect.Type;
import java.util.ArrayList;

import kotlin.Unit;
import kotlin.jvm.functions.Function2;


/**
 * Description： 问诊消息ViewHolder
 * Date:2023/11/29 15:16
 */
public class InquiryViewHolder extends FunChatBaseMessageViewHolder {
    public static final String TAG = "InquiryViewHolder";
    ChatMessageInquiryViewHolderBinding binding;
    InquiryAttachment attachment;

    ImPictureAdapter imPictureAdapter;


    public InquiryViewHolder(@NonNull ChatBaseMessageViewHolderBinding parent, int viewType) {
        super(parent, viewType);
    }

    @Override
    protected void addViewToMessageContainer() {
        binding = ChatMessageInquiryViewHolderBinding.inflate(LayoutInflater.from(parent.getContext()), getMessageContainer(), true);

    }


    @SuppressLint("SetTextI18n")
    @Override
    public void bindData(ChatMessageBean message, ChatMessageBean lastMessage) {
        super.bindData(message, lastMessage);
        currentMessage = message;
        attachment = (InquiryAttachment) message.getMessageData().getMessage().getAttachment();
        if (attachment == null) {
            return;
        }


        if (!attachment.getDiseaseDescription().isEmpty()){

            binding.rcyDrug.setVisibility(View.VISIBLE);
            binding.rcyDrug.setLayoutManager(new FullyGridLayoutManager(parent.getContext(),3, FullyGridLayoutManager.VERTICAL, false));
            imPictureAdapter = new ImPictureAdapter();
            binding.rcyDrug.setAdapter(imPictureAdapter);
            imPictureAdapter.setData(attachment.getDiseaseDescription());
            imPictureAdapter.setOnItemClickListener((view, integer) -> {
                ArrayList<LocalMedia> loacls = new ArrayList<>();
                LocalMedia localMedia;
                for (int i = 0; i < imPictureAdapter.getData().size(); i++) {
                    localMedia = new LocalMedia();
                    localMedia.setPath(imPictureAdapter.getData().get(i));
                    loacls.add(localMedia);
                }

                PreviewPicActivity.Companion.start(parent.getContext(), integer, loacls);
                return null;
            });


        }else {
            //这里必须对RecyclerView进行隐藏
            binding.rcyDrug.setVisibility(View.GONE);
        }



        //设置ViewHolder展示UI的内容
        binding.tvTypeName.setText(attachment.getITypeName());
        binding.tvName.setText(attachment.getPatientName());
        binding.describe.setText("病情描述："+attachment.getDescribe());
        binding.ifReturnVisit.setText("是否复诊："+attachment.getIfReturnVisit());
        binding.illnessDuration.setText("患病时长："+attachment.getIllnessDuration());
        binding.lastDiagnosis.setText("上次确诊诊断："+attachment.getLastDiagnosis());
        binding.lastVisitHospital.setText("上次就医医院："+attachment.getLastVisitHospital());
        binding.lastVisitDate.setText("上次就诊日期："+attachment.getLastVisitDate());
        binding.ifSanity.setText("是否神志清楚："+attachment.getIfSanity());
        binding.inHospitalHere.setText("曾在本院住院："+attachment.getInHospitalHere());
        binding.inpatientDepartment.setText("住院科室："+attachment.getInpatientDepartment());
        binding.inpatientDate.setText("住院日期："+attachment.getInpatientDate());

        Glide.with(parent.getContext()).load(attachment.getPatientAvatar())
                .skipMemoryCache(false) //启用内存缓存
                .diskCacheStrategy(DiskCacheStrategy.RESOURCE) //磁盘缓存策略
                .apply(RequestOptions.circleCropTransform()) // 圆形
                .into(binding.ivHead);

        binding.tvAge.setText(attachment.getPatientAge());
        binding.ivSex.setBackground(attachment.getSex().equals("1") ? getDrawableResource(R.mipmap.icon_male) : getDrawableResource(R.mipmap.icon_female));




    }


    @Override
    protected void onMessageBackgroundConfig(ChatMessageBean messageBean) {
        super.onMessageBackgroundConfig(messageBean);
        boolean isReceivedMsg = MessageHelper.isReceivedMessage(messageBean);
        View firstChild = baseViewBinding.messageContainer.getChildAt(0);
        if (isReceivedMsg) {
            firstChild.setBackgroundResource(R.drawable.bg_custom_message);
        } else {
            firstChild.setBackgroundResource(R.drawable.bg_custom_message_out);
        }
    }
}
